package com.coffee.soft.controller;

import com.alibaba.fastjson.JSON;
import com.coffee.soft.entity.ItemsDetail;
import com.coffee.soft.entity.UserAssess;
import com.coffee.soft.entity.UsersItems;
import com.coffee.soft.service.ItemsDetailPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author:LiuRuidong
 * @Description: 商品详情页
 * @Date: Created in 15:47 2018/2/5
 * @Modified By:
 */
@Controller
public class ItemsDetailPageController {

    @Autowired
    private ItemsDetailPageService itemsDetailPageService;
/**
 * @Author：LiuRuidong
 * @Description: 商品信息详情
 * @Date:2018/2/5 22:56
 * @Param:
 * @return
 */
    @RequestMapping("/itemsDetail")
    @ResponseBody
    public ItemsDetail changeNewImageHandler(HttpServletRequest request, HttpServletResponse response,String itemsId,String userId) {

        String callback = request.getParameter("callback");
        UsersItems usersItems = new UsersItems();
        usersItems.setItemsId(itemsId);
        usersItems.setUserId(userId);
        ItemsDetail itemsDetail =itemsDetailPageService.showItemsDatail(usersItems);
        String json = JSON.toJSONString(itemsDetail);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();

            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return itemsDetail;
    }
    /**
     * @Author：LiuRuidong
     * @Description: 返回收藏状态，-1表示已满，0表示未收藏，1表示已收藏
     * @Date:2018/2/5 23:09
     * @Param:
     * @return
     */
    @RequestMapping("/itemsCollection")
    @ResponseBody
    public void collectionStatus(HttpServletRequest request, HttpServletResponse response,String itemsId,String userId){
        /*response.setContentType("text/plain");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Access-Control-Allow-Origin", "*");//添加跨域访问*/
        String callback = request.getParameter("callback");
        UsersItems usersItems = new UsersItems();
        usersItems.setItemsId(itemsId);
        usersItems.setUserId(userId);
        int result =itemsDetailPageService.MyCollectionStatus(usersItems);
        String json = JSON.toJSONString(result);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Author：LiuRuidong
     * @Description:  商品用户评价
     * @Date:2018/2/5 23:14
     * @Param:
     * @return
     */
    @RequestMapping("/usersAssess")
    @ResponseBody
    public  void itemsUsersAssesss(HttpServletRequest request, HttpServletResponse response,String itemsId, String userId) {
        String callback = request.getParameter("callback");
        UsersItems usersItems = new UsersItems();
        usersItems.setItemsId(itemsId);
        usersItems.setUserId(userId);
        List<UserAssess> userAssessList =itemsDetailPageService.ItemsUsersAssess(usersItems);
        String json = JSON.toJSONString(userAssessList);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Author：LiuRuidong
     * @Description: 加入足迹表
     * @Date:2018/2/5 23:21
     * @Param:
     * @return
     */
    @RequestMapping("/footprintService")
    @ResponseBody
    public void footprint(String itemsId, String userId) {
        UsersItems usersItems = new UsersItems();
        usersItems.setItemsId(itemsId);
        usersItems.setUserId(userId);
        itemsDetailPageService.MyFootprintService(usersItems);
    }

    /**
     * @Author：LiuRuidong
     * @Description: 购物车状态
     * @Date:2018/2/5 23:38
     * @Param:
     * @return
     */
    @RequestMapping("/shoppingCar")
    @ResponseBody
    public Boolean shoppingCartSatus(String itemsId, String userId) {
        UsersItems usersItems = new UsersItems();
        usersItems.setItemsId(itemsId);
        usersItems.setUserId(userId);
        Boolean bool = itemsDetailPageService.shoppingCart(usersItems);
        return bool;
    }

}
